package org.ticlib.taskwrapper
{
	import org.ticlib.task.Task;

	public class TaskWrapper
	{
		
		[Bindable] protected var uniqueId:String;
		[Bindable] public var task:Task;
		[Bindable] protected var wrappers:Array;
		[Bindable] protected var resultFunction:Function;
		[Bindable] protected var faultFunction:Function;
		[Bindable] protected var cancelFunction:Function;
		[Bindable] protected var connectFunction:Function;
		
		
		public function TaskWrapper(id:String = "")
		{
			uniqueId = id;
			wrappers = new Array();
		}
		
		public function add(wrapper:TaskWrapper):TaskWrapper 
		{
			wrapper.result(resultFunction);
			wrapper.cancel(cancelFunction);
			wrapper.fault(faultFunction);
			
			wrappers.push(wrapper);
			return this;
		}
		
		public function result(resultFunction:Function):TaskWrapper
		{
			this.resultFunction = resultFunction;
			for each(var itm:TaskWrapper in wrappers)
			{
				itm.result(resultFunction);
			}
			return this;
		}
		
		public function fault(faultFunction:Function):TaskWrapper
		{
			this.faultFunction = faultFunction;
			for each(var itm:TaskWrapper in wrappers)
			{
				itm.fault(faultFunction);
			}
			return this;
		}
		
		public function cancel(cancelFunction:Function):TaskWrapper
		{
			this.cancelFunction = cancelFunction;
			for each(var itm:TaskWrapper in wrappers)
			{
				itm.cancel(cancelFunction);
			}
			return this;
		}
		
		public function connect(connectFunction:Function):TaskWrapper
		{
			this.connectFunction = connectFunction;
			for each(var itm:TaskWrapper in wrappers)
			{
				itm.connectFunction(connectFunction);
			}
			return this;
		}
		
		public function execute():void
		{
			
		}
	}
}